function r = automation(sbar,stilde,p)

% compute maximal comparative advantage of capital relative to stilde:
xmax = fminbnd(@(x) loglaborprod(stilde,x,p)-logcapprod(x,p),0,1);

% compute xup:
if costcompup(sbar,stilde,automationlow(sbar,stilde,xmax,p),xmax,p)<=1 % stilde is cheaper than capital at xmax
    
    xup = xmax;
    
else
    
    xup = fzero(@(x) costcompup(sbar,stilde,automationlow(sbar,stilde,x,p),x,p)-1,[xmax 1-(1-stilde)*p.stol],p.optionsfzero);
    
end

% collect results:
r.xup = xup;
r.xlow = automationlow(sbar,stilde,xup,p);
r.xmax = xmax;

end